package net.huashitong.synthetical.gradePoint.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import net.huashitong.synthetical.gradePoint.entity.GradePoint;
import net.huashitong.synthetical.gradePoint.filter.GradePointFilter;
import net.huashitong.synthetical.gradePoint.service.GradePointService;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 绩点考核
 */
@Controller
@RequestMapping("/gradepoint")
public class GradePointCtrl extends JsonBaseController {

    @Autowired
    private GradePointService service;
    @Override
    protected String getView(String s) {
        return "/modules/gradepoint/" + s;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {

        return getView("query");
    }

    /**
     * 初始化数据
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute GradePointFilter filter) {
        List<GradePoint> list = service.queryByFilter(filter);
        //处理考核类别
        for(GradePoint gradePoint : list){
            if("1".equals(gradePoint.getCheckType())){
                gradePoint.setCheckType("个人");
            }
            if("2".equals(gradePoint.getCheckType())){
                gradePoint.setCheckType("单位");
            }
            if("3".equals(gradePoint.getCheckType())){
                gradePoint.setCheckType("督查组");
            }
        }
        return coverJqGrid(filter, list);
    }

    /**
     * 添加
     * @param model
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.GET)
    public String showAdd(Model model){
        GradePointFilter filter = new GradePointFilter();
        model.addAttribute("filter",filter);
        return getView("/form");
    }
    @ResponseBody
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public Message add(@ModelAttribute GradePointFilter filter){
        GradePoint gradePoint = new GradePoint();
        UtilsTools.copyPro(filter,gradePoint);
        service.save(gradePoint);
        return coverMessage("200");
    }

    /**
     * 修改
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit/{id}")
    public String showUpdate(Model model,@PathVariable String id){
        GradePoint gradePoint = service.getById(id);
        model.addAttribute("filter",gradePoint);
        return getView("/form");
    }
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Message update(@ModelAttribute GradePointFilter filter){
        GradePoint gradePoint = new GradePoint();
        UtilsTools.copyPro(filter,gradePoint);
        service.update(gradePoint);
        return coverMessage("200");
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}")
    public Message delete(@PathVariable List<String> id){
        service.batchDelete(id);
        return coverMessage("200");
    }


    @RequestMapping("/filter")
    public String filter() {
        return getView("filter");
    }

    /**
     * 修改
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/showDetail/{id}")
    public String showDetail(Model model,@PathVariable String id){
        GradePoint gradePoint = service.getById(id);
        if("1".equals(gradePoint.getCheckType())){
            gradePoint.setCheckType("个人");
        }
        if("2".equals(gradePoint.getCheckType())){
            gradePoint.setCheckType("单位");
        }
        if("3".equals(gradePoint.getCheckType())){
            gradePoint.setCheckType("督查组");
        }

        model.addAttribute("gradePoint",gradePoint);
        return getView("/form");
    }
}
